<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :page="page"
      @row-update="rowUpdate"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template
        slot="regionType"
        v-if="regionTypeList.length > 0"
        slot-scope="scope"
      >
        <div>{{ regionTypeFilter(scope.row.regionType) }}</div>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import {
  getList,
  remove,
  update,
  add,
  getRegionType,
} from "@/api/system/tenant";
import { mapGetters } from "vuex";
import { getLazyTree } from "@/api/base/region";

export default {
  data() {
    return {
      form: {
        address: [],
      },
      selectionList: [],
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: "auto",
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: true,
        delBtn: false,
        addBtn: false,
        column: [
          {
            label: "租户ID",
            prop: "tenantId",
            search: true,
            addDisplay: false,
            editDisplay: false,
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入租户ID",
                trigger: "blur",
              },
            ],
          },
          {
            label: "租户名称",
            prop: "tenantName",
            search: true,
            span: 24,
            labelWidth: "110",
            rules: [
              {
                required: true,
                message: "请输入参数名称",
                trigger: "blur",
              },
            ],
          },
          {
            label: "租户头像",
            prop: "imgUrl",
            type: "upload",
            listType: "picture-img",
            tip: "请上传 .png,.jpg 格式图片",
            action: "/api/blade-file/file/uploadFile",
            propsHttp: {
              res: "data",
              url: "fileUrl",
            },
            span: 24,
            row: true,
            labelWidth: "110",
          },
          {
            label: "联系人",
            prop: "linkman",
            search: true,
            span: 24,
            labelWidth: "110",
            rules: [
              {
                required: true,
                message: "请输入联系人",
                trigger: "blur",
              },
            ],
          },
          {
            label: "联系电话",
            prop: "contactNumber",
            span: 24,
            labelWidth: "110",
          },
          {
            label: "督查",
            prop: "linkmanTwo",
            span: 24,
            labelWidth: "110",
          },
          {
            label: "行政区域",
            prop: "regionName",
            span: 24,
            minRows: 6,
            type: "textarea",
            display: false,
          },
          {
            label: "行政区域",
            prop: "regionCodes",
            span: 24,
            minRows: 6,
            type: "cascader",
            hide: true,
            labelWidth: "110",
            props: {
              label: "title",
              value: "value",
            },
            lazy: true,
            lazyLoad(node, resolve) {
              const parentCode = node.level === 0 ? "00" : node.data.id;
              getLazyTree(parentCode).then((res) => {
                resolve(
                  res.data.data.map((item) => {
                    return {
                      ...item,
                      leaf: !item.hasChildren,
                    };
                  })
                );
              });
            },
          },
          {
            label: "联系地址",
            prop: "domain",
            span: 24,
            labelWidth: "110",
          },
          // {
          //   label: "教育管理部门",
          //   prop: "regionType",
          //   type: "select",
          //   dicUrl: "/api/blade-system/dict/dictionary?code=region_scope",
          //   slot: true,
          //   props: {
          //     label: "dictValue",
          //     value: "dictKey",
          //   },
          //   span: 24,
          //   labelWidth: "110",
          //   rules: [
          //     {
          //       required: true,
          //       message: "请选择教育管理部门",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          // {
          //   label: "租户类型",
          //   prop: "schoolType",
          //   type: "select",
          //   dataType: "string",
          //   dicUrl: "/api/blade-system/dict/dictionary?code=schoolType",
          //   props: {
          //     label: "dictValue",
          //     value: "dictKey",
          //   },
          //   span: 24,
          //   labelWidth: "110",
          //   rules: [
          //     {
          //       required: true,
          //       message: "请选择教育管理部门",
          //       trigger: "blur",
          //     },
          //   ],
          // },
        ],
      },
      data: [],
      regionTypeList: [],
      tenantInfo: {},
    };
  },
  created() {
    let info = JSON.parse(localStorage.getItem("saber-userInfo")).content;
    this.tenantInfo = info;
    getRegionType().then((res) => {
      this.regionTypeList = res.data.data;
    });
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.tenant_add, false),
        viewBtn: this.vaildData(this.permission.tenant_view, false),
        delBtn: this.vaildData(this.permission.tenant_delete, false),
        editBtn: this.vaildData(this.permission.tenant_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      let id = JSON.parse(localStorage.getItem("saber-userInfo")).content
        .tenantId;
      params.tenantId = id;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.data.map((item) => {
          if (item.regionType) item.regionType = item.regionType * 1;
        });
        // console.log(this.data);
        this.loading = false;
      });
    },
    regionTypeFilter(val) {
      let regionTypeName = "";
      this.regionTypeList.forEach((item) => {
        if (val == item.dictKey) {
          regionTypeName = item.dictValue;
        }
      });

      return regionTypeName;
    },
  },
  filters: {},
};
</script>

<style></style>
